
package com.ss.security;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.ss.bean.User;

/**
 * 用户获取
 * @author ddh
 *
 */
@Component("myUserDetailService")
public class MyUserDetailService extends HibernateDaoSupport implements UserDetailsService {
	@Autowired
    public void init(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
     @Override
    public UserDetails loadUserByUsername(String userName)
            throws UsernameNotFoundException, DataAccessException {
    	 
    	 List<User> users = getHibernateTemplate().find(
 				"FROM User user WHERE user.name = ? AND user.disabled = false",
 				userName);
    	 
 		if (users.isEmpty()) {
 			throw new UsernameNotFoundException("User " + userName
 					+ " has no GrantedAuthority");
 		}
 		
 		User dbUser = users.get(0);
 		
 		boolean enabled = dbUser.isDisabled();
 		boolean accountNonExpired = true;
 		boolean credentialsNonExpired = true;
 		boolean accountNonLocked = true;
 		//new spring user
//         User(String username, String password, boolean enabled, boolean accountNonExpired,
//                     boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities) {
 		
 		return dbUser;
 		
     }
    
}